package com.student.course.controller;

import com.student.common.entity.Result;
import com.student.common.entity.Course;
import com.student.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 课程控制器
 * 
 * @author student
 * @date 2025
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    /**
     * 课程列表查询
     */
    @GetMapping("/list")
    public Result<Page<Course>> getCourseList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String courseType,
            @RequestParam(required = false) String courseId,
            @RequestParam(required = false) String status) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<Course> courses = courseService.getCourseList(pageable, courseName, department, courseType, courseId, status);
            return Result.success(courses);
        } catch (Exception e) {
            return Result.error("查询课程列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询课程
     */
    @GetMapping("/{id}")
    public Result<Course> getCourseById(@PathVariable String id) {
        try {
            Course course = courseService.getCourseById(id);
            if (course != null) {
                return Result.success(course);
            } else {
                return Result.error("课程不存在");
            }
        } catch (Exception e) {
            return Result.error("查询课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增课程
     */
    @PostMapping
    public Result<String> addCourse(@RequestBody Course course) {
        try {
            boolean success = courseService.addCourse(course);
            if (success) {
                return Result.success("新增课程成功");
            } else {
                return Result.error("新增课程失败，课程编号已存在");
            }
        } catch (Exception e) {
            return Result.error("新增课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新课程信息
     */
    @PutMapping("/{id}")
    public Result<String> updateCourse(@PathVariable String id, @RequestBody Course course) {
        try {
            course.setCourseId(id);
            boolean success = courseService.updateCourse(course);
            if (success) {
                return Result.success("更新课程信息成功");
            } else {
                return Result.error("更新课程信息失败，课程不存在");
            }
        } catch (Exception e) {
            return Result.error("更新课程信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCourse(@PathVariable String id) {
        try {
            boolean success = courseService.deleteCourse(id);
            if (success) {
                return Result.success("删除课程成功");
            } else {
                return Result.error("删除课程失败，课程不存在或有选课记录");
            }
        } catch (Exception e) {
            return Result.error("删除课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据教师ID查询课程列表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<Page<Course>> getCoursesByTeacherId(
            @PathVariable String teacherId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<Course> courses = courseService.getCoursesByTeacherId(pageable, teacherId);
            return Result.success(courses);
        } catch (Exception e) {
            return Result.error("查询教师课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取课程总数
     */
    @GetMapping("/count")
    public Result<Long> getCourseCount() {
        try {
            long count = courseService.getCourseCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取课程总数失败：" + e.getMessage());
        }
    }
}
